<?php

namespace App\System\Logic;

use Hyperf\Di\Annotation\Inject;
use App\System\Service\HeiDeptService;
use Haozing\FastCore\Constants\ErrorCode;
use Haozing\FastCore\Exception\CommonException;

class HeiDeptLogic
{
    #[Inject]
    protected HeiDeptService $heiDeptService;
    /**
     * 新增逻辑
     * @param array $params
     * @return array
     */
    public function add(array $params = []): array
    {
        // 参数处理
        $data = [
            'tenant_id' => $params['tenant_id'] ?? 0,
            'parent_id' => $params['parent_id'] ?? 0,
            'level' => $params['level'] ?? '',
            'name' => $params['name'] ?? '',
            'leader' => $params['leader'] ?? '',
            'phone' => $params['phone'] ?? '',
            'status' => $params['status'] ?? 0,
            'sort' => $params['sort'] ?? 0,
            'created_by' => $params['created_by'] ?? 0,
            'updated_by' => $params['updated_by'] ?? 0,
            'created_at' => date('Y-m-d H:i:s'),
            'remark' => $params['remark'] ?? '',

        ];
        // TODO 逻辑处理
        $data = $this->heiDeptService->addOne($data);
        if (!$data) {
            throw new CommonException(ErrorCode::BAD_REQUEST,'新增失败');
        }
        return [];

    }

    /**
     * 更新逻辑
     * @param array $params
     * @return int
     */
    public function update(array $params = []): int
    {
        // 条件参数处理
        $where = [];
        // 更新数据参数处理
        $data = [];
        if (isset($params['tenant_id'])) {
            $data['tenant_id'] = $params['tenant_id'];
        }
        if (isset($params['parent_id'])) {
            $data['parent_id'] = $params['parent_id'];
        }
        if (isset($params['level'])) {
            $data['level'] = $params['level'];
        }
        if (isset($params['name'])) {
            $data['name'] = $params['name'];
        }
        if (isset($params['leader'])) {
            $data['leader'] = $params['leader'];
        }
        if (isset($params['phone'])) {
            $data['phone'] = $params['phone'];
        }
        if (isset($params['status'])) {
            $data['status'] = $params['status'];
        }
        if (isset($params['sort'])) {
            $data['sort'] = $params['sort'];
        }
        if (isset($params['created_by'])) {
            $data['created_by'] = $params['created_by'];
        }
        if (isset($params['updated_by'])) {
            $data['updated_by'] = $params['updated_by'];
        }
        if (isset($params['created_at'])) {
            $data['created_at'] = $params['created_at'];
        }
        if (isset($params['remark'])) {
            $data['remark'] = $params['remark'];
        }

        // TODO 逻辑处理
        $data = $this->heiDeptService->updateByWhere($where,$data);
        if (!$data) {
            throw new CommonException(ErrorCode::BAD_REQUEST,'修改失败');
        }
        return $data;
    }

    /**
     * 删除逻辑
     * @param array $params
     * @return mixed
     */
    public function delete(array $params = []): array
    {
        // TODO 逻辑处理
        $data = $this->heiDeptService->deleteByWhere($params);
        if (!$data) {
            throw new CommonException(ErrorCode::BAD_REQUEST,'删除失败');
        }
        return [];
    }

    /**
     * 详情逻辑
     * @param array $params
     * @return array
     */
    public function detail(array $params = []): array
    {
        // TODO 逻辑处理
        if (!isset($params['id']) ||!$params['id']) {
            throw new CommonException(ErrorCode::INVALID_PARAM,'参数错误');
        }
        $data = $this->heiDeptService->getOneById($params['id'],['*']);
        if (!$data) {
            throw new CommonException(ErrorCode::NOT_FOUND,'资源不存在');
        }
        return $data;
    }

    /**
     * 列表查询逻辑
     * @param array $params
     * @return array
     */
    public function list(array $params = []): array
    {
        // 条件参数处理
        $where = [];
        // 其他参数处理
        $options = [];
        if (isset($params['tenant_id']) && $params['tenant_id']) {
            $where[] = ['tenant_id',$params['tenant_id']];
        }
        if (isset($params['parent_id']) && $params['parent_id']) {
            $where[] = ['parent_id',$params['parent_id']];
        }
        if (isset($params['level']) && $params['level']) {
            $where[] = ['level',$params['level']];
        }
        if (isset($params['name']) && $params['name']) {
            $where[] = ['name',$params['name']];
        }
        if (isset($params['leader']) && $params['leader']) {
            $where[] = ['leader',$params['leader']];
        }
        if (isset($params['phone']) && $params['phone']) {
            $where[] = ['phone',$params['phone']];
        }
        if (isset($params['status']) && $params['status']) {
            $where[] = ['status',$params['status']];
        }
        if (isset($params['sort']) && $params['sort']) {
            $where[] = ['sort',$params['sort']];
        }
        if (isset($params['created_by']) && $params['created_by']) {
            $where[] = ['created_by',$params['created_by']];
        }
        if (isset($params['updated_by']) && $params['updated_by']) {
            $where[] = ['updated_by',$params['updated_by']];
        }
        if (isset($params['created_at']) && $params['created_at']) {
            $where[] = ['created_at',$params['created_at']];
        }
        if (isset($params['remark']) && $params['remark']) {
            $where[] = ['remark',$params['remark']];
        }


        if (isset($params['pageSize']) && $params['pageSize']) {
            $options['pageSize'] = $params['pageSize'];
        }
        // TODO 逻辑处理
        $data = $this->heiDeptService->getPage($where,['*'],$options);
        // TODO 数据聚合
        return [
            'total' => $data['total']??0,
            'list' => $data['data']??[],
        ];
    }
    /**
     * 列表查询[简单kv字段]
     * @param array $params
     * @return array
     */
    public function select(array $params = []): array
    {
        // 条件参数处理
        $where = [];
        // 其他参数处理
        $options = [];
        if (isset($params['tenant_id']) && $params['tenant_id']) {
            $where[] = ['tenant_id',$params['tenant_id']];
        }
        if (isset($params['parent_id']) && $params['parent_id']) {
            $where[] = ['parent_id',$params['parent_id']];
        }
        if (isset($params['level']) && $params['level']) {
            $where[] = ['level',$params['level']];
        }
        if (isset($params['name']) && $params['name']) {
            $where[] = ['name',$params['name']];
        }
        if (isset($params['leader']) && $params['leader']) {
            $where[] = ['leader',$params['leader']];
        }
        if (isset($params['phone']) && $params['phone']) {
            $where[] = ['phone',$params['phone']];
        }
        if (isset($params['status']) && $params['status']) {
            $where[] = ['status',$params['status']];
        }
        if (isset($params['sort']) && $params['sort']) {
            $where[] = ['sort',$params['sort']];
        }
        if (isset($params['created_by']) && $params['created_by']) {
            $where[] = ['created_by',$params['created_by']];
        }
        if (isset($params['updated_by']) && $params['updated_by']) {
            $where[] = ['updated_by',$params['updated_by']];
        }
        if (isset($params['created_at']) && $params['created_at']) {
            $where[] = ['created_at',$params['created_at']];
        }
        if (isset($params['remark']) && $params['remark']) {
            $where[] = ['remark',$params['remark']];
        }


        if (isset($params['pageSize']) && $params['pageSize']) {
            $options['pageSize'] = $params['pageSize'];
        }
        // TODO 逻辑处理
        $data = $this->heiDeptService->getPage($where,['id','name'],$options);
        // TODO 数据聚合
        return [
            'total' => $data['total']??0,
            'list' => $data['data']??[],
        ];
    }
}